package edu.gatech.cs2340.teamrocket.view;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.util.List;
import java.awt.image.BufferedImage;
import edu.gatech.cs2340.teamrocket.model.Model;
import edu.gatech.cs2340.teamrocket.model.map.Cave;
import edu.gatech.cs2340.teamrocket.model.map.City;
import edu.gatech.cs2340.teamrocket.model.map.Desert;
import edu.gatech.cs2340.teamrocket.model.map.Forest;
import edu.gatech.cs2340.teamrocket.model.map.Lakeside;
import edu.gatech.cs2340.teamrocket.model.map.Location;
import edu.gatech.cs2340.teamrocket.model.map.Mountain;
import edu.gatech.cs2340.teamrocket.model.map.Plains;
import edu.gatech.cs2340.teamrocket.model.map.Town;
/**
 * This generates a visual representation of the Game Map
 *
 * @author Wai Chip Ngai
 */
@SuppressWarnings("serial")
public class VisualMap extends Canvas{
	private List<Location> locations;
	private Model model;


	public VisualMap(List<Location> locations, Model model){
		this.locations = locations;
		this.model = model;
		setPreferredSize (getPerferredSize());
	}

	private void drawLocation(Graphics2D g, Location loc, int x, int y, boolean isCurrent, boolean isOdd){
		int NAME_SPACE = 40;
		int startDrawingX = x;
		int startDrawingY = y;
		int size = 15;
		if (loc instanceof Cave){
			g.setColor(Color.black);
			if (isOdd){
				g.drawString(loc.getName(), startDrawingX, y-NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y+NAME_SPACE);
					g.drawString("You are here", startDrawingX, y+NAME_SPACE);
				}

			}else{
				g.drawString(loc.getName(), startDrawingX, y+NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y-NAME_SPACE);
					g.drawString("You are here", startDrawingX, y-NAME_SPACE);
				}
			}
			g.drawOval(startDrawingX, startDrawingY, size,size);
		}else if(loc instanceof City){
			g.setColor(Color.black);
			g.fillOval(startDrawingX, startDrawingY, size, size);
			if (isOdd){
				g.drawString(loc.getName(), startDrawingX, y-NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y+NAME_SPACE);
					g.drawString("You are here", startDrawingX, y+NAME_SPACE);
				}

			}else{
				g.drawString(loc.getName(), startDrawingX, y+NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y-NAME_SPACE);
					g.drawString("You are here", startDrawingX, y-NAME_SPACE);
				}
			}
		}else if(loc instanceof Desert){
			g.setColor(Color.blue);
			g.drawOval(startDrawingX, startDrawingY, size, size);
			if (isOdd){
				g.drawString(loc.getName(), startDrawingX, y-NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y+NAME_SPACE);
					g.drawString("You are here", startDrawingX, y+NAME_SPACE);
				}

			}else{
				g.drawString(loc.getName(), startDrawingX, y+NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y-NAME_SPACE);
					g.drawString("You are here", startDrawingX, y-NAME_SPACE);
				}
			}
		}else if(loc instanceof Forest){
			g.setColor(Color.red);
			g.drawOval(startDrawingX, startDrawingY, size, size);
			if (isOdd){
				g.drawString(loc.getName(), startDrawingX, y-NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y+NAME_SPACE);
					g.drawString("You are here", startDrawingX, y+NAME_SPACE);
				}

			}else{
				g.drawString(loc.getName(), startDrawingX, y+NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y-NAME_SPACE);
					g.drawString("You are here", startDrawingX, y-NAME_SPACE);
				}
			}
		}else if(loc instanceof Lakeside){
			g.setColor(Color.red);
			g.drawRect(startDrawingX, startDrawingY, size, size);
			if (isOdd){
				g.drawString(loc.getName(), startDrawingX, y-NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y+NAME_SPACE);
					g.drawString("You are here", startDrawingX, y+NAME_SPACE);
				}

			}else{
				g.drawString(loc.getName(), startDrawingX, y+NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y-NAME_SPACE);
					g.drawString("You are here", startDrawingX, y-NAME_SPACE);
				}
			}
		}else if(loc instanceof Mountain){
			g.setColor(Color.blue);
			g.drawRect(startDrawingX, startDrawingY, size, size);
			if (isOdd){
				g.drawString(loc.getName(), startDrawingX, y-NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y+NAME_SPACE);
					g.drawString("You are here", startDrawingX, y+NAME_SPACE);
				}

			}else{
				g.drawString(loc.getName(), startDrawingX, y+NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y-NAME_SPACE);
					g.drawString("You are here", startDrawingX, y-NAME_SPACE);
				}
			}
		}else if(loc instanceof Plains){
			g.setColor(Color.black);
			g.drawRect(startDrawingX, startDrawingY, size, size);
			if (isOdd){
				g.drawString(loc.getName(), startDrawingX, y-NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y+NAME_SPACE);
					g.drawString("You are here", startDrawingX, y+NAME_SPACE);
				}

			}else{
				g.drawString(loc.getName(), startDrawingX, y+NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y-NAME_SPACE);
					g.drawString("You are here", startDrawingX, y-NAME_SPACE);
				}
			}
		}else if(loc instanceof Town){
			g.setColor(Color.blue);
			g.fillRect(startDrawingX, startDrawingY, size, size);
			if (isOdd){
				g.drawString(loc.getName(), startDrawingX, y-NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y+NAME_SPACE);
					g.drawString("You are here", startDrawingX, y+NAME_SPACE);
				}

			}else{
				g.drawString(loc.getName(), startDrawingX, y+NAME_SPACE);
				if (isCurrent){
					g.drawLine(x, y, x, y-NAME_SPACE);
					g.drawString("You are here", startDrawingX, y-NAME_SPACE);
				}
			}
		}
	}

	public void drawLegend(Graphics2D g ){
		int startDrawingX = 5;
		int startDrawingY = 500;
		int size = 15;
		int separation = 75;
		g.setColor(Color.blue);
		g.fillRect(startDrawingX, startDrawingY, size, size);
		g.drawString("Town", startDrawingX, startDrawingY);
		startDrawingX += separation;
		g.setColor(Color.black);
		g.drawRect(startDrawingX, startDrawingY, size, size);
		g.drawString("Plains", startDrawingX, startDrawingY);
		startDrawingX += separation;
		g.setColor(Color.blue);
		g.drawRect(startDrawingX, startDrawingY, size, size);
		g.drawString("Mountain", startDrawingX, startDrawingY);
		startDrawingX += separation;
		g.setColor(Color.red);
		g.drawRect(startDrawingX, startDrawingY, size, size);
		g.drawString("Lakeside", startDrawingX, startDrawingY);
		startDrawingX += separation;
		g.setColor(Color.red);
		g.drawOval(startDrawingX, startDrawingY, size, size);
		g.drawString("Forest", startDrawingX, startDrawingY);
		startDrawingX += separation;
		g.setColor(Color.blue);
		g.drawOval(startDrawingX, startDrawingY, size, size);
		g.drawString("Desert", startDrawingX, startDrawingY);
		startDrawingX += separation;
		g.setColor(Color.black);
		g.fillOval(startDrawingX, startDrawingY, size, size);
		g.drawString("City", startDrawingX, startDrawingY);
		startDrawingX += separation;
		g.setColor(Color.black);
		g.drawOval(startDrawingX, startDrawingY, size,size);
		g.drawString("Cave", startDrawingX, startDrawingY);
		startDrawingX += separation;
		g.drawString("Filled: With Shop", 5, 550);
		g.drawString("Empty: No Shop", 200, 550);

	}


	public BufferedImage getVisualMap() {
		BufferedImage temp = new BufferedImage((locations.size()+1)*60, 600, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = temp.createGraphics();
		int LocNum = 1;

		for (Location loc: locations){
			boolean isCurrent = model.getCurrentLocation().equals(loc);
			int xPos = 10+loc.getEndDist()*4; ;
			int yPos = (int)(Math.pow(-1, LocNum))*100+250;
			if(LocNum %2 ==0){
				drawLocation(g2, loc, xPos-7, yPos+7,isCurrent, false);
			}else{
				drawLocation(g2, loc, xPos-7, yPos-7,isCurrent, true);
			}

			g2.setColor(Color.black);
			if(!loc.equals(locations.get(locations.size() - 1))){
				int nextYPos = (int)(Math.pow(-1, LocNum+1))*100+250;
				int nextXPos = 10+locations.get(locations.indexOf(loc)+1).getEndDist()*4;
				if (LocNum %2 ==0){
					g2.drawLine(xPos+15, yPos-15,nextXPos-15, nextYPos+15);
				}else{
					g2.drawLine(xPos+15, yPos+15,nextXPos-15, nextYPos-15);
				}
				//int [] xPoints = [nextXPos, ]
				//g.dr
			}
			LocNum++;
			g2.drawString("You have Traveled "+model.getDistanceTraveled()+"km", 5, 450);
		}

		drawLegend(g2);
		return temp;
	}

	private void drawBackground(){

	}

	private Dimension getPerferredSize(){
		int x = (locations.size()+1)*60;
		int y = 700;
		return new Dimension(x,y);
	}

}
